﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
using System.IO;

namespace HRM
{
    public partial class frmQuaTrinhBHXH : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _employeeID = 0;
        int _PremiumID = 0;
        int _opt = 0;
        EmployeeClient _wcfEmployee;
        PremiumClient _wcfPremium;
        string _InsBookNo = "";
        RoleRightInfo listRole;
        bool _IsLoaded = false;
        DateTime _ViewDate = DateTime.MinValue;

        public frmQuaTrinhBHXH(int EmployeeID, string InsBookNo, DateTime _DateVal, int opt)
        {
            InitializeComponent();
            _wcfEmployee = Service.Employee();
            _wcfPremium = Service.Premium();
            listRole = UIParamater.Quyen_PhatSinhBaoHiem;
            this.KeyPreview = true;
            _ViewDate = _DateVal;
            _employeeID = EmployeeID;
            _InsBookNo = InsBookNo;
            _opt = opt;
            AddColumnPremium();
            LoadPremium(_InsBookNo);
        }

        private void AddColumnPremium()
        {
            //UISetting.AddColumn(ref dgvPremium, "Premium_ID", "Mã chính", "", 1, false, true);
            //UISetting.AddColumn(ref dgvPremium, "RowNumber", UpdateUI.GetString("stt"), "C", 30, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_WorkUnit", UpdateUI.GetString("donvilamviec"), "", 150, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_FromDate", "Từ tháng", "C", 80, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_ToDate", "Đến tháng", "C", 80, true, true);
            UISetting.AddColumn(ref dgvPremium, "LoaiPs", "Loại", "C", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_Job", UpdateUI.GetString("nghenghiep"), "", 100, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_Salary", "ML", "R", 70, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_BasicFactor", "HSL", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_PositionFactor", "HS Cvụ", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_SeniorityFactor", "HS TNNghề", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_OverSeniorityFactor", "HS TNVK", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_AreaFactor", "HS KV", "R", 60, false, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_MonthNo", "Số tháng", "R", 60, true, true);
            UISetting.AddColumn(ref dgvPremium, "Premium_Description", UpdateUI.GetString("ghichu"), "", 100, false, true);
            //UISetting.AddColumn(ref dgvPremium, "Detail_ID", "Detail_ID", "", 30, false, true);
        }

        private void LoadPremium(string _SIBookNo)
        {
            try
            {
                DataTable dt = new DataTable();
                PremiumClient _wcfPremium = Service.Premium();
                dt = _wcfPremium.Premium_QTBH(Program.strBranchCode, _SIBookNo, _opt, DateTime.MinValue, DateTime.MaxValue,0).Tables[0];
                //dt = _wcfPremium.Premium_GetBySIBookNo(_SIBookNo, true).Tables[0];
                dgvPremium.Rows.Clear();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dgvPremium.Rows.Insert(i, 1);
                        //dgvList.Columns["Premium_ToDate"].Frozen = true;
                        //dgvPremium.Rows[i].Cells["Premium_ID"].Value = dt.Rows[i]["Premium_ID"].ToString();
                        //dgvPremium.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_WorkUnit"].Value = dt.Rows[i]["Premium_WorkUnit"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_FromDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]).ToString("MM/yyyy");
                        dgvPremium.Rows[i].Cells["Premium_ToDate"].Value = Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"]).ToString("MM/yyyy");
                        dgvPremium.Rows[i].Cells["LoaiPs"].Value = dt.Rows[i]["LoaiPs"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_Job"].Value = dt.Rows[i]["Premium_Job"].ToString();
                        dgvPremium.Rows[i].Cells["Premium_Salary"].Value = dt.Rows[i]["Premium_Salary"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_Salary"]).ToString("#,##0");
                        dgvPremium.Rows[i].Cells["Premium_BasicFactor"].Value = dt.Rows[i]["Premium_BasicFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_BasicFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_PositionFactor"].Value = dt.Rows[i]["Premium_PositionFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_PositionFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_SeniorityFactor"].Value = dt.Rows[i]["Premium_SeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_SeniorityFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_OverSeniorityFactor"].Value = dt.Rows[i]["Premium_OverSeniorityFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_OverSeniorityFactor"]).ToString("#0.0##");
                        dgvPremium.Rows[i].Cells["Premium_AreaFactor"].Value = dt.Rows[i]["Premium_AreaFactor"] == DBNull.Value ? "0" : Convert.ToDecimal(dt.Rows[i]["Premium_AreaFactor"]).ToString("#0.0##");
                        if (dt.Rows[i]["Premium_MonthNo"].ToString() != "0")
                            dgvPremium.Rows[i].Cells["Premium_MonthNo"].Value = dt.Rows[i]["Premium_MonthNo"].ToString();
                        else
                            dgvPremium.Rows[i].Cells["Premium_MonthNo"].Value = (UISetting.GetMonth(Convert.ToDateTime(dt.Rows[i]["Premium_FromDate"]), Convert.ToDateTime(dt.Rows[i]["Premium_ToDate"])) + 1).ToString();
                        dgvPremium.Rows[i].Cells["Premium_Description"].Value = dt.Rows[i]["Premium_Description"].ToString();
                        //dgvPremium.Rows[i].Cells["Detail_ID"].Value = dt.Rows[i]["Detail_ID"].ToString();
                        //if (i == 0)
                        //    dgvPremium.Rows[i].Cells["Premium_ToDate"].Value = _ViewDate.ToString("MM/yyyy");
                        if (dgvPremium.Rows[i].Cells["Premium_WorkUnit"].Value.ToString() == "")
                        {
                            dgvPremium.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                            dgvPremium.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                            //MergerCell
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(UpdateUI.GetString("loi") + ":\n" + ex.Message + "\n" + UpdateUI.GetString("ghiloi"));
            }
        }

        //private void PageIndex()
        //{
        //    totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
        //    if (totalPage == 0)
        //        lblPageIndex.Text = "0" + "/" + totalPage.ToString();
        //    else
        //        lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        //}

        //private void btnFisrt_Click(object sender, EventArgs e)
        //{
        //    currPage = 0;
        //    _IsLoaded = false;
        //    LoadEmployeeData();
        //}

        //private void btnNext_Click(object sender, EventArgs e)
        //{
        //    if (totalPage > 0 && currPage < totalPage - 1)
        //    {
        //        currPage++;
        //    }
        //    _IsLoaded = false;
        //    LoadEmployeeData();
        //}

        //private void btnPrevious_Click(object sender, EventArgs e)
        //{
        //    if (currPage > 0)
        //        currPage--;
        //    _IsLoaded = false;
        //    LoadEmployeeData();
        //}

        //private void btnLast_Click(object sender, EventArgs e)
        //{
        //    if (totalPage > 0)
        //        currPage = totalPage - 1;
        //    _IsLoaded = false;
        //    LoadEmployeeData();
        //}

        //private void dgvPremiumList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex >= 0)
        //        btnEdit_Click(null, null);
        //}

        private void frmQuaTrinhBHXH_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
                case Keys.F2:
                    btnExcel_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmQuaTrinhBHXH_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfEmployee.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            frmTime f = new frmTime(1);
            string fileName = "QTBHXH.xls";
            DataTable dt = new DataTable();

            if (f.ShowDialog(this) == DialogResult.OK)
            {
                dt = _wcfPremium.Premium_GetByDate(f.FromDate, f.ToDate, Program.strBranchCode, 1, "").Tables[0];
                try
                {
                    if (dt.Rows.Count > 0)
                    {
                        ExportData2Excel(dt, Path.GetTempPath() + "\\" + fileName);
                        try
                        {
                            System.Diagnostics.Process.Start(Path.GetTempPath() + "\\" + fileName);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + fileName, "Thông báo");
                        }
                    }
                    else
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), "Không có dữ liệu!", UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                }
                catch (Exception)
                {
                    MessageBox.Show("File " + Path.GetTempPath() + "\\" + fileName + " đang được mở bằng một ứng dụng khác.\nHãy chọn file khác!", "Thông báo");
                }
            }
        }

        private void ExportData2Excel(DataTable dt, string _FileName)
        {
            if (dt.Columns.Contains("RowNumber"))
                dt.Columns["RowNumber"].Caption = "STT";

            if (dt.Columns.Contains("Premium_SIBookNo"))
                dt.Columns["Premium_SIBookNo"].Caption = "SOBHXH";

            if (dt.Columns.Contains("Employee_FullName"))
                dt.Columns["Employee_FullName"].Caption = "HOTEN";

            if (dt.Columns.Contains("Premium_WorkUnit"))
                dt.Columns["Premium_WorkUnit"].Caption = "MADVI";

            if (dt.Columns.Contains("Premium_WorkName"))
                dt.Columns["Premium_WorkName"].Caption = "TENDVI";

            if (dt.Columns.Contains("Premium_Job"))
                dt.Columns["Premium_Job"].Caption = "NGHENGHIEP";

            if (dt.Columns.Contains("MaNTe"))
                dt.Columns["MaNTe"].Caption = "MA_NTE";

            if (dt.Columns.Contains("Premium_FromDate"))
                dt.Columns["Premium_FromDate"].Caption = "TUTHANG";

            if (dt.Columns.Contains("Premium_ToDate"))
                dt.Columns["Premium_ToDate"].Caption = "DENTHANG";

            if (dt.Columns.Contains("Premium_Salary"))
                dt.Columns["Premium_Salary"].Caption = "ML";

            if (dt.Columns.Contains("Premium_BasicFactor"))
                dt.Columns["Premium_BasicFactor"].Caption = "HSL";

            if (dt.Columns.Contains("Premium_PositionFactor"))
                dt.Columns["Premium_PositionFactor"].Caption = "PC1";

            if (dt.Columns.Contains("Premium_OverSeniorityFactor"))
                dt.Columns["Premium_OverSeniorityFactor"].Caption = "PC2";

            if (dt.Columns.Contains("Premium_SeniorityFactor"))
                dt.Columns["Premium_SeniorityFactor"].Caption = "PC3";

            if (dt.Columns.Contains("Premium_AreaFactor"))
                dt.Columns["Premium_AreaFactor"].Caption = "PC4";

            if (dt.Columns.Contains("Premium_InsTypeCode"))
                dt.Columns["Premium_InsTypeCode"].Caption = "PA";

            ReadExcelFile.exportDataToExcel(dt, _FileName);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmTime frm = new frmTime(2);
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                frmPreviewReport f = new frmPreviewReport("InQTBHXH", new string[] { _employeeID.ToString(), frm.FromDate.ToString("dd/MM/yyyy"), frm.ToDate.ToString("dd/MM/yyyy") });
                f.Show(this);
            }
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            if (_employeeID > 0)
            {

            }
        }
    }
}
